var vo={};
$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listRepair").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight-10);
	//动态设置改变操作列宽度
	var fcol=opt.split('</a>');
	var contrlColumn=0;
	if(fcol.length>0){
		contrlColumn=(fcol.length-1)*window.fcolMinWidth;
	}
	
	$("#listRepair").datagrid({
					url:'/repair/getRepair',
					title:'检修列表',
					method:'get',
					pagination:true,
					pageList: window.PageSize,
					loadMsg:'加载中......',
					nowrap:true,
					rowStyler:function(index,row){   
				        if (row.isflag){   
				            return 'background-color:'+window.flagColor;   
				        }
				    },
					columns:[[
					         {field:'ck',checkbox:true},
							 {field:'id' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
								 var re=new RegExp("{param}","g");
								 var ref=opt.replace(re,row.id);
								 return ref;
							 }},
							 {field:'title',title:'检修标题', align:'left',width:100},
							 {field:'repairtime',title:'检修时间',align:'left',width:80,formatter:function(value,row,index){
					        	 if(value==''){
					        		 return '';
					        	 }
					        	 var datetime=Common.formatterDate(value);
					        	 return datetime;
					         }},
					         {field:'deviceid',title:'设备编号', align:'left',width:100},
					         {field:'devicename',title:'设备名称', align:'left',width:100},
					         {field:'cost',title:'检修费用', align:'left',width:100},
					         {field:'invitation',title:'耗材', align:'left',width:200},
					         {field:'remark',title:'备注', align:'left',width:200},
					     ]]
				});
	$("#serbutton").click(function(){
		vo.deviceid=$("#deviceid").val();
		vo.devicename=$("#devicename").val();
		$("#listRepair").datagrid('load',vo);
	});
	$("#listRepair").datagrid({
		toolbar: '#tb'
	});
	//绑定回车事件
	$(document).keydown(function(event){ 
		if(event.keyCode==13){
			$("#serbutton").click(); 
		}
		if(event.keyCode==46){
			var rows=$("#listRepair").datagrid('getSelections');
			if(rows.length==1){
				delRepair(rows[0].repairid);
			}
			else{
				betchDel();
			}
		}
		if(event.keyCode==67){
			var rows=$("#listRepair").datagrid('getSelections');
			if(rows.length<=0){
				$.messager.alert("失败","选中的记录为空：",'error');
			}
			copyRepair(rows[0].repairid);
		}
	})
	//如果操作项为空隐藏操作列
	if(opt==''){
		$("#listRepair").datagrid('hideColumn','id');
	}
});
/**
 * 批量删除
 * @param idcard
 */
function betchDel(){
	var rows=$("#listRepair").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('删除','您确定要删除选中的检修吗？',function(data){
		if(data){
			var ids='';
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].id+',';
			}
			$.ajax({
				type:'get',
				url: '/repair/betchDel',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listRepair").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 删除
 * @param idcard
 */
function delRepair(id){
	$.messager.confirm('删除','您确定要删除选中的检修吗？',function(data){
		if(data){
			$.ajax({
				type:'get',
				url: '/repair/delRepair',
				data:{id:id},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listRepair").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 详情
 */
function viewRepair(id){
	window.parent.openDialog("检修详情",'/repair/viewRepair','',400,500,'id='+id);
}
/**
 * 新增
 */
function addRepair(){
	window.parent.openDialog("新增检修",'/repair/addRepair','',400,500,'');
}
/**
 * 复制
 */
function copyRepair(id){
	window.parent.openDialog("新增检修",'/repair/copyRepair','',400,500,'id='+id);
}
/**
 * 编辑
 * @param id
 */
function edit(id){
	window.parent.openDialog("修改检修",'/repair/editRepair','',400,500,'id='+id);
}
/**
 * 导出为excel
 */
function exportXls(){
	vo.devicename=getVal(vo.devicename);
	vo.deviceid=getVal(vo.deviceid);
	var url='/repair/exportXls?deviceid='+vo.deviceid+'&devicename'+vo.devicename;
	window.open(url,'','',false);
}
